﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ParallelRuntimeLibrary;
using System.Threading;
using System.Diagnostics;

namespace TestProject
{
    public static class TasksTest
    {

        public static void Test()
        {
            op:
            Queue<int> aa = new Queue<int>();
            int Count = 0;

            Action<object> method = new Action<object>(oo => 
            {
                int o = (int)oo;
                for (int i = 0; i < o; i++)
			    {
    			    lock(aa)
                        aa.Enqueue(i);
			    }

                Action<object> method2 = new Action<object>(ooo =>
                    {
                        int psa = 0;
                        for (int i = 0; i < 1024 * 1024; i++)
                        {
                            psa = i;
                            psa += i;
                            psa = (i / 2) - psa;
                        }

                        Action<object> method3 = new Action<object>(oooo =>
                            {
                                for (int i = 0; i < 102 * 104; i++)
                                {
                                    psa = i;
                                    psa += i;
                                    psa = (i / 2) - psa;
                                }
                            });

                        Task[] baseT3 = new Task[6];
                        for (int ii = 0; ii < baseT3.Length; ii++)
                        {
                            baseT3[ii] = new Task(method3, null);
                        }
                        TaskManager.WaitAll(baseT3);

                    });

                Task[] baseT = new Task[4];
                for (int ii = 0; ii < baseT.Length; ii++)
                {
                    baseT[ii] = new Task(method2, null);
                }
                TaskManager.WaitAll(baseT);

                for (int i = 0; i < o / 2; i++)
			    {
    			    lock (aa)
                        aa.Dequeue();
			    }

                Interlocked.Increment(ref Count);

            });

            var sw = Stopwatch.StartNew();

            Task[] t = new Task[16];
            for (int i = 0; i < t.Length; i++)
                t[i] = new Task(method, i);

            TaskManager.WaitAll(t);

            sw.Stop();

            Console.WriteLine(Count);
            Console.WriteLine("Time : " + sw.ElapsedMilliseconds);
            Console.ReadLine();
            goto op;
        }

    }
}
